<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/5/15
 * Time: 17:53
 */
namespace app\home\controller;
use think\Controller;
use think\Db;
class IndexController extends Controller
{
    public function index(){
        header("Access-Control-Allow-Origin:*");
        $data = Db::table('ew_index')
            ->value('image');
        $data = trim($data,',');
        $photo =explode(',',$data);
        foreach ($photo as $key=>$vo){
            $photo[$key] = '/static/uploads/images/'.$vo;
        }
        //=====================================================
        $name = 'userSelf';
        if(isset($_REQUEST['name']) && $_REQUEST['name']){
            $name = $_REQUEST['name'];
        }
        //=====================================================
        $this->assign ('photo',$photo);
        $this->assign ('name',$name);
        return $this->fetch('index/index');
    }
    public function long(){
        return $this->fetch('index/long');
    }
    public function video($who){
        $this->assign ('who',$who);
        return $this->fetch('index/video');
    }
    public function getSignPackage() {
            $jsapiTicket = $this-> getJsApiTicket();
            $url = "http://jiehu.xuanwumobile.com/home/index/getBanner.html";
            $timestamp = time();
            $nonceStr = $this->createNonceStr();
            // 这里参数的顺序要按照 key 值 ASCII 码升序排序
            $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
            $signature = sha1($string);
            $signPackage = array(
                "appId"     => 'wx08496717d84737e8',
                "nonceStr"  => $nonceStr,
                "timestamp" => $timestamp,
                "url"       => $url,
                "signature" => $signature,
                "rawString" => $string
            );
            return json_encode($signPackage);
        }
        private function getJsApiTicket() {
            $data = json_decode(session("jsapi_ticket"));
            if (empty($data)) {
                $accessToken = $this->getAccessToken();
                $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
                $res = json_decode($this->httpGet($url));
                $ticket = $res->ticket;
                if ($ticket) {
                    $data['jsapi_ticket'] = $ticket;
                    session("jsapi_ticket", json_encode($data));
                }}else{
                $ticket = $data->jsapi_ticket;
            }
            return $ticket;
        }

        private function createNonceStr($length = 16) {
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            $str = "";
            for ($i = 0; $i < $length; $i++) {
                $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
            }
            return $str;
        }

        private function getAccessToken() {
            $data = json_decode(session("access_token"));
            if (empty($data)) {
                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx08496717d84737e8&secret=d77f2394edbaca428db6b65894161f9a";
                $res = json_decode($this->httpGet($url));

                $access_token = $res->access_token;
                if ($access_token) {
                    $data['access_token'] = $access_token;
                    session("access_token", json_encode($data), 7000);
                }
            }else{
                $access_token = $data->access_token;
            }
            return $access_token;
    }
    private function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_URL, $url);
        // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
        // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($curl, CURLOPT_HEADER, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

        $res = curl_exec($curl);
        curl_close($curl);

        return $res;
    }
}
